package droideep.library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import droideep.library.R;
import droideep.library.utils.MetricsConverter;

/**
 * @author goodev
 * @date 15/7/3
 */
public class BoundaryView extends View {

    private CharSequence mBoundaryTitle = "";
    private int mBoundaryTitleColor = Color.BLACK;
    private int mBoundaryColor = Color.parseColor("#34daa9");
    private final float TEXT_SIZE = MetricsConverter.dpToPx(getContext(), 14.0f);
    private final float PADDING = MetricsConverter.dpToPx(getContext(), 16.0f);
    private final float LINE_HEIGHT = MetricsConverter.dpToPx(getContext(), 2.0f);

    public BoundaryView(Context context) {
        super(context);
    }

    public BoundaryView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BoundaryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BoundaryView);
        if (ta.hasValue(R.styleable.BoundaryView_android_text)) {
            mBoundaryTitle = ta.getString(R.styleable.BoundaryView_android_text);
        }

        mBoundaryColor = ta.getColor(R.styleable.BoundaryView_android_color, mBoundaryColor);

        mBoundaryTitleColor = ta.getColor(R.styleable.BoundaryView_android_textColor, mBoundaryTitleColor);

        ta.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width = 0;
        int height;

        // 宽度匹配父窗口
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        }

        // 高度等于字体的高度
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = (int) TEXT_SIZE;
        }

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int sizeWidth = getMeasuredWidth();
        int sizeHeight = getMeasuredHeight();

        // 画text
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
        textPaint.setTextSize(TEXT_SIZE);
        textPaint.setColor(mBoundaryTitleColor);
        textPaint.setTextAlign(Paint.Align.CENTER);

        String text = mBoundaryTitle.toString();
        final Rect targetRect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), targetRect);
        targetRect.top = 0;
        targetRect.bottom = sizeHeight;

        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();

        float baseline = targetRect.top + (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
        canvas.drawText(text, sizeWidth / 2, baseline, textPaint);

        /**
         * 获取画{@link text}的宽度
         */
        final float textWidth = targetRect.width();
        // 计算线的宽度
        final float lineWidth = (sizeWidth - PADDING * 2 - textWidth) / 2;

        // 画左线
        float leftL = 0;
        float topL = (sizeHeight - LINE_HEIGHT) / 2;
        float rightL = leftL + lineWidth;
        float bottomL = topL + LINE_HEIGHT;
        Paint leftLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        leftLinePaint.setColor(mBoundaryColor);
        canvas.drawRect(leftL, topL, rightL, bottomL, leftLinePaint);

        // 画右线
        float leftR = sizeWidth - lineWidth;
        float topR = (sizeHeight - LINE_HEIGHT) / 2;
        float rightR = leftR + lineWidth;
        float bottomR = topR + LINE_HEIGHT;
        Paint rightLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        rightLinePaint.setColor(mBoundaryColor);
        canvas.drawRect(leftR, topR, rightR, bottomR, rightLinePaint);
    }

    public CharSequence getBoundaryTitle() {
        return mBoundaryTitle;
    }

    public void setBoundaryTitle(CharSequence boundaryTitle) {
        mBoundaryTitle = boundaryTitle;
        invalidate();
    }
}
